/*
 * shapDraw.cpp
 *
 *  Created on: 04-Sep-2011
 *      Author: jay
 */

#include"shapDraw.h"
#include<shapefil.h>
#include <GL/freeglut.h>


//Function to Open Shapefile and parse the info
void OpenShapeFile(char* fileName)
{

    SHPHandle hSHP=SHPOpen(fileName, "r" );

    if(hSHP == NULL) return;
    printf("point type: %d\n",hSHP->nShapeType);
  //  printf("point: 34\n");
	//Read Bounding Box of Shapefile
    sBoundingBox.fMaxX=hSHP->adBoundsMax[0];
    sBoundingBox.fMaxY=hSHP->adBoundsMax[1];

    sBoundingBox.fMinX=hSHP->adBoundsMin[0];
    sBoundingBox.fMinY=hSHP->adBoundsMin[1];





	//Point Shapefile
    if(hSHP->nShapeType == SHPT_POINT)
    {
        SHPObject *psShape;
        for(int i=0;i<hSHP->nRecords;i++)
        {
            psShape = SHPReadObject(hSHP, i);

            double fX = psShape->padfX[0];
            double fY = -psShape->padfY[0];

            //Plot these points
            MyPoint2D pt;
            pt.dX=fX;
            pt.dY=-fY;
            vPoints.push_back(pt);
//            std::cout<<"point :"<<pt.dX<<"\n";
//            printf("point: %f\n",psShape->padfZ);
        }
    }


	//Line Shapefile
    else if(hSHP->nShapeType == SHPT_ARC)
    {
        SHPObject *psShape;
	    for(int i=0;i<hSHP->nRecords;i++)
        {
            psShape = SHPReadObject(hSHP, i);
            vector<MyPoint2D> tempPointArray;

            for(int j=0;j<psShape->nVertices;j++)
            {
				double fX = psShape->padfX[j];
				double fY = psShape->padfY[j];
		//		cout<<"z:"<<psShape->padfZ[j];
				MyPoint2D pt;
				pt.dX=fX;
				pt.dY=fY;
				tempPointArray.push_back(pt);
            }

			MyLineString2D linestring;
            linestring.vPointList=tempPointArray;
            vLines.push_back(linestring);

        }
    }

 	//Polygon Shapefile
    if(hSHP->nShapeType == SHPT_POLYGON)
    {
		SHPObject *psShape;
		for(int i=0;i<hSHP->nRecords;i++)
		{
			psShape = SHPReadObject(hSHP, i);
      		vector<MyPoint2D> tempPointArray;

			for(int j=0;j<psShape->nVertices;j++)
			{
				double fX = psShape->padfX[j];
				double fY = psShape->padfY[j];
				MyPoint2D pt;
				pt.dX=fX;
				pt.dY=fY;
      			tempPointArray.push_back(pt);
			}
			MyPolygon2D polygon;
			polygon.vPointList=tempPointArray;
			vPolygons.push_back(polygon);
		}

    }

    if(hSHP->nShapeType == SHPT_POINTZ ){
         SHPObject *psShape;
        for(int i=0;i<hSHP->nRecords;i++)//hSHP->nRecords
        {
            psShape = SHPReadObject(hSHP, i);
           
            double fX = psShape->padfX[0];
            double fY = psShape->padfY[0];
            double fz = psShape->padfZ[0];

            //Plot these points
            MyPoint3D pt;
            pt.dX=fX;
            pt.dY=fY;
            pt.dZ=fz;
            v3Points.push_back(pt);
            

        }
      }
      if(hSHP->nShapeType == SHPT_POLYGONZ ){
            SHPObject *psShape;
		printf("Record:%d\n",hSHP->nRecords);
		for( int i = 0; i < hSHP->nRecords; i++){
			psShape = SHPReadObject(hSHP,i);
			vector<MyPoint3D> tempPointArray;
			for( int j = 0; j < psShape->nVertices; j++ ){
				printf("%f %f %f\n",psShape->padfX[j],psShape->padfY[j],psShape->padfZ[j]);
				MyPoint3D pt;
				pt.dX = psShape->padfX[j];
				pt.dY = psShape->padfY[j];
				pt.dZ = psShape->padfZ[j];
				tempPointArray.push_back(pt);
			}
			MyPolygon3D polygon;
			polygon.v3PointList = tempPointArray;
			v3Polygons.push_back(polygon);
		}
    
            }
}

void initializeGL()
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
//glClearColor (1.0, 1.0, 1.0, 1.0);
	glShadeModel (GL_FLAT);
	glEnable (GL_LINE_SMOOTH);
	glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_LINE_SMOOTH);

	//Assign Default Map Bounds to glOrtho
	sBoundingBox.fMaxX=10000.0f;//180.0f;
	sBoundingBox.fMaxY=10000.0f;//90.0f;
	sBoundingBox.fMinX=-10000.0f;//-180.0f;
	sBoundingBox.fMinY=-10000.0f;//-90.0f;
        
}


void resizeGL(int w, int h)
{
	if(h<=0) h=1 ;
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	//Assign Bounding Box Coordinates of Shapefile to glOrtho()
	glOrtho(sBoundingBox.fMinX, sBoundingBox.fMaxX,sBoundingBox.fMinY,sBoundingBox.fMaxY,-1000,1000);
	glMatrixMode(GL_MODELVIEW);
}


void draw()
{
	int i;

	glClear (GL_COLOR_BUFFER_BIT);
	glColor3f (0.0, 0.0, 1.0);
	glLoadIdentity ();

	//Render Point Shapefile
	glColor3f (1.0, 0.0, 0.0);
	glEnable(GL_POINT_SMOOTH) ;
	glPointSize(5.0);
	glBegin(GL_POINTS);
	for(i=0;i<(int)v3Points.size();i++)//(int)v3Points.size()
	{
		glVertex3f(v3Points[i].dX,v3Points[i].dY,v3Points[i].dZ);
	}

	glEnd();

	//Render Line Shapefile
	glColor3f (0.0, 1.0, 0.0);

	for(i=0;i<(int)v3Lines.size();i++)
	{

		glBegin(GL_LINE_STRIP);
		for(int j=0;j<(int)v3Lines[i].v3PointList.size();j++)
		{
		  glVertex3f(v3Lines[i].v3PointList[j].dX,v3Lines[i].v3PointList[j].dY,v3Lines[i].v3PointList[j].dZ);

		}

		glEnd();
	}

	//Render Polygon Shapefile
	glColor3f(1.0,0.0, 0.0);
	for( i=0;i<(int)vPolygons.size();i++)
	{
		glBegin(GL_LINE_LOOP);
		for(int j=0;j<(int)vPolygons[i].vPointList.size();j++)
		{
			glVertex2f(vPolygons[i].vPointList[j].dX,vPolygons[i].vPointList[j].dY);
		}

		glEnd();
	}

	glColor3f(0.0,.5, 0.5);
	for( i=0;i<(int)v3Polygons.size();i++)
	{
		glBegin(GL_LINE_LOOP);

		for(int j=0;j<(int)v3Polygons[i].v3PointList.size();j++)
		{
			glVertex3f(v3Polygons[i].v3PointList[j].dX,v3Polygons[i].v3PointList[j].dY,v3Polygons[i].v3PointList[j].dZ);
		}

		glEnd();
	}

   glFlush();
}

void paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    draw();
}

